package com.jd.jr.pay.demo.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jd.jr.pay.demo.model.PortalAutoVO;
import com.jd.jr.pay.demo.util.MD5Util;
import com.jd.jr.pay.demo.util.PropertyUtils;
import com.jd.jr.pay.demo.util.StringEscape;

@Controller
public class portalController {

	private static final Logger logger = Logger.getLogger(portalController.class);

	/**
	 * 授信开始页面
	 * @param httpServletRequest
	 * @return
	 */
	@RequestMapping(value = "/portalStart.htm")
	public String portal(HttpServletRequest httpServletRequest) {
		httpServletRequest.setAttribute("mercId", PropertyUtils.getProperty("9f.mercId"));
		httpServletRequest.setAttribute("uMblNo", PropertyUtils.getProperty("9f.user.uMblNo"));
		httpServletRequest.setAttribute("redirect_uri", PropertyUtils.getProperty("9f.merc.portalPage"));
		return "portalStart";
	}
	
	/**
	 * 授信跳回页面
	 * @param httpServletRequest
	 * @return
	 */
	@RequestMapping(value = "/portalFinishPage.htm")
	public String portalFinishPage(HttpServletRequest httpServletRequest) {
		return "portalFinishPage";
	}
	
	
	/**
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 * 授信自提交代码
	 */
	@RequestMapping(value = "/portalAuto.htm")
	public String portalAuto(ModelMap map, HttpServletRequest request, HttpServletResponse response) throws Exception {
		PortalAutoVO portalAutoVO = new PortalAutoVO();
		
		portalAutoVO.setCharset(request.getParameter("charset"));
		portalAutoVO.setMercId(request.getParameter("mercId"));
		portalAutoVO.setInterfaceName(request.getParameter("interfaceName"));
		portalAutoVO.setVersion(request.getParameter("version"));
		portalAutoVO.setSignType(request.getParameter("signType"));
		
		
		portalAutoVO.setuMblNo(request.getParameter("uMblNo"));
		portalAutoVO.setRedirect_uri(request.getParameter("redirect_uri"));
		
		//过滤null值
		filterPortalVO(portalAutoVO);
		
		//生成验签
		String paySignString = getPortalSign(portalAutoVO, PropertyUtils.getProperty("9f.salt"));
		portalAutoVO.setHmac(MD5Util.md5(paySignString));
		
		
		request.setAttribute("portalAutoVO", portalAutoVO);
		request.setAttribute("url", PropertyUtils.getProperty("9f.portal.url"));

		return "portalAutoSubmit";
	}

	/**
	 * 
	 * @Title: filterCharProcess
	 * @Description: 对值中的<>*&特殊字符进行转义
	 * @param: @param basePayOrderInfo
	 * @return: void
	 * @throws
	 * @author mythling
	 * @Date 2016年8月6日 下午3:55:17
	 */
	private void filterPortalVO(PortalAutoVO portalAutoVO) {
		portalAutoVO.setCharset(this.doFilterCharProcess(portalAutoVO.getCharset()));
		portalAutoVO.setMercId(this.doFilterCharProcess(portalAutoVO.getMercId()));
		portalAutoVO.setInterfaceName(this.doFilterCharProcess(portalAutoVO.getInterfaceName()));
		portalAutoVO.setVersion(this.doFilterCharProcess(portalAutoVO.getVersion()));
		portalAutoVO.setSignType(this.doFilterCharProcess(portalAutoVO.getSignType()));
		
		portalAutoVO.setuMblNo(this.doFilterCharProcess(portalAutoVO.getuMblNo()));
		portalAutoVO.setRedirect_uri(this.doFilterCharProcess(portalAutoVO.getRedirect_uri()));
	}
	public String getPortalSign(PortalAutoVO portalAutoVO,String key){
		return portalAutoVO.getCharset()+
				portalAutoVO.getMercId()+
				portalAutoVO.getInterfaceName()+
				portalAutoVO.getVersion()+
				portalAutoVO.getSignType()+
				portalAutoVO.getuMblNo()+
				key;
	}

	/**
	 * 
	 * @Title: doFilterCharProcess
	 * @Description: 执行特殊字符处理
	 * @param: @param param
	 * @param: @return
	 * @return: String
	 * @throws
	 * @author mythling
	 * @Date 2016年8月6日 下午3:54:58
	 */
	private String doFilterCharProcess(String param) {
		if (param == null || param.equals("")) {
			return "";
		} else {
			return StringEscape.htmlSecurityEscape(param);
		}
	}


}
